package com.xgry.bpm.api.base.moduler.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgry.bpm.api.base.moduler.entity.TBModuler;
import com.xgry.bpm.api.base.moduler.form.TBModulerForm;
import com.xgry.bpm.api.base.moduler.service.TBModulerService;
import com.xgry.bpm.api.base.moduler.vo.TBModulerVO;
import com.xgry.bpm.common.utils.EasyPoiUtils;
import com.xgry.bpm.common.utils.MiniUITreeUtil;
import com.xgry.bpm.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * TBModuler前端控制器
 *
 * </p>
 *
 * @author admin
 * @since 2020-07-14
 */
@RestController
@Api(tags = "TBModuler")
@RequestMapping("/tBModuler")
public class TBModulerController {

    @Autowired
    public TBModulerService tBModulerService;

    /**
     * 保存单条
     *
     * @param param 保存参数
     * @return 是否添加成功
     */
    @ApiOperation(value = "保存", notes = "保存数据到TBModuler")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_name", value = "模块名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_url", value = "模块url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_icon", value = "模块图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "i_moduler_sort", value = "模块排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_code", value = "模块编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_state", value = "模块状态 0:启用 1：停用", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gateway_id", value = "门户id", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gateway_name", value = "门户名称", required = false, dataType = ""),
    })
    @PostMapping(value = "/add.do")
    public Result addTBModuler(@RequestBody TBModulerForm param) {
        if (1 != tBModulerService.save(param)) {
            return Result.fail("保存失败");
        }
        return Result.success();
    }

    @ApiOperation(value = "通过模块名称查询模块是否存在", notes = "通过模块名称查询模块是否存在")
    @ApiImplicitParam(paramType = "query", name = "modulerName", value = "模块名称", required = true)
    @PostMapping("/findModulerByName")
    public Result findModulerByName(@RequestParam("modulerName") String modulerName) {
        TBModuler tbModuler = tBModulerService.findModelByName(modulerName);
        if (null != tbModuler) {
            return Result.fail("模块名称已存在，请重新输入");
        }
        return Result.success("success");
    }

    @ApiOperation(value = "通过模块编码查询模块是否存在", notes = "通过模块编码查询模块是否存在")
    @ApiImplicitParam(paramType = "query", name = "modulerCode", value = "模块编码", required = true)
    @PostMapping("/findModulerByCode")
    public Result findModulerByCode(@RequestParam("modulerCode") String modulerCode) {
        TBModuler tbModuler = tBModulerService.findModelByCode(modulerCode);
        if (null != tbModuler) {
            return Result.fail("模块编码已存在，请重新输入");
        }
        return Result.success("success");
    }

    @ApiOperation(value = "将模块排序上移", notes = "将模块排序上移")
    @ApiImplicitParam(paramType = "query", name = "modulerId", value = "模块主键id", required = true)
    @PostMapping("/moveUpward")
    @Transactional
    public Result moveUpward(@RequestParam("modelId") Integer modulerId) {
        TBModuler tbModuler = tBModulerService.moveUpModuler(modulerId);
        if (null == tbModuler) {
            return Result.fail("当前模块没有上一层数据");
        }
        return Result.success("success");
    }

    @ApiOperation(value = "将模块排序下移", notes = "将模块排序下移")
    @ApiImplicitParam(paramType = "query", name = "modulerId", value = "模块主键id", required = true)
    @PostMapping("/moveDownward")
    @Transactional
    public Result moveDownward(@RequestParam("modelId") Integer modulerId) {
        TBModuler tbModuler = tBModulerService.moveDownModuler(modulerId);
        if (null == tbModuler) {
            return Result.fail("当前模块没有上一层数据");
        }
        return Result.success("success");
    }


    /**
     * 更新(根据主键id更新)
     *
     * @param param 修改参数
     * @return 是否更改成功
     */
    @ApiOperation(value = "更新数据", notes = "根据主键id更新TBModuler数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_name", value = "模块名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_url", value = "模块url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_icon", value = "模块图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "i_moduler_sort", value = "模块排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_code", value = "模块编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_state", value = "模块状态 0:启用 1：停用", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gateway_id", value = "门户id", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gateway_name", value = "门户名称", required = false, dataType = ""),
    })
    @PostMapping(value = "/updateById.do")
    public Result updateTBModulerById(@RequestBody TBModulerForm param) {
        if (1 != tBModulerService.updateById(param)) {
            return Result.fail("保存失败");
        }
        return Result.success();
    }


    /**
     * 删除(根据主键id删除)
     *
     * @param //主键id
     * @return 是否删除成功
     */
    @ApiOperation(value = "删除数据", notes = "根据主键id删除TBModuler数据")
    @ApiImplicitParam(paramType = "query", name = "", value = "Id", required = true, dataType = "String")
    @PostMapping(value = "/deleteById.do")
    @Transactional
    public Result deleteTBModulerById(String[] ids) {
        int result = 0;
        for (String id : ids) {
            tBModulerService.deleteById(id);
            result++;
        }

        if (result > 0) {
            return Result.fail("删除失败！");
        }

        return Result.success("操作成功");
    }

    /**
     * 根据主键id查询单条
     *
     * @param // 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "获取单条数据", notes = "根据主键id获取TBModuler数据")
    @ApiImplicitParam(paramType = "query", name = "", value = "Id", required = true, dataType = "String")
    @PostMapping(value = "/getById.do")
    public Result getTBModulerById(@RequestParam(value = "Id") String id) {
        TBModulerVO result = tBModulerService.selectById(id);
        if (null == result) {
            return Result.fail("无数据！");
        }
        return Result.success("操作成功", result);
    }

    /**
     * 查询全部
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "全部查询", notes = "查询TBModuler全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_name", value = "模块名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_url", value = "模块url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_icon", value = "模块图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "i_moduler_sort", value = "模块排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_code", value = "模块编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_state", value = "模块状态 0:启用 1：停用", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gateway_id", value = "门户id", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gateway_name", value = "门户名称", required = false, dataType = ""),
    })
    @RequestMapping(value = "/queryAll.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getTBModulerAll(@RequestBody TBModulerForm param) {
        List<TBModulerVO> result = tBModulerService.selectAll(param);
        if (CollectionUtils.isEmpty(result)) {
            return Result.fail("无数据！");
        }
        return Result.success("操作成功", result);
    }

    /**
     * 分页查询
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询", notes = "分页查询TBModuler全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_by", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_name", value = "模块名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_url", value = "模块url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_icon", value = "模块图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "i_moduler_sort", value = "模块排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_code", value = "模块编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_moduler_state", value = "模块状态 0:启用 1：停用", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gateway_id", value = "门户id", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "gateway_name", value = "门户名称", required = false, dataType = ""),
    })
    @RequestMapping(value = "/queryPage.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getTBModulerPage(@RequestBody TBModulerForm param) {
        IPage page = tBModulerService.page(new Page<>(0,10), new QueryWrapper<TBModuler>() {{
            this.setEntity(new TBModuler());
        }});
        IPage<TBModulerVO> result = tBModulerService.selectPage(param);
        if (CollectionUtils.isEmpty(result.getRecords())) {
            return Result.fail("无数据！");
        }
        return Result.convertIpageToSuccess(result);
    }

    /**
     * Excel导入
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "Excel导入", notes = "导入全字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "", value = "", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "", value = "", required = false, dataType = "String")
    })
    @RequestMapping(value = "/importExcel.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result importExcel(@RequestParam MultipartFile file) throws Exception {
        List<TBModuler> list = EasyPoiUtils.importExcel(file, 0, 1, TBModuler.class);
        if (CollectionUtils.isEmpty(list)) {
            return Result.fail("无数据");
        }
        return Result.success();
    }

    /**
     * Excel导出
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "Excel导出", notes = "导出全字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "current", value = "页码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "size", value = "每页条数", required = false, dataType = "String")

    })
    @RequestMapping(value = "/exportExcel.do", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public void exportExcel(HttpServletResponse response) throws Exception {
        List<TBModuler> result = tBModulerService.list();
        EasyPoiUtils.exportExcel(result, "模块表列表", "模块表报表", TBModuler.class, "模块表明细报表.xls", response);

    }

    @ApiOperation(value = "获取模块树结构", notes = "模块树形结构")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/getTBOrgTree.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getRoleTree(@RequestBody TBModulerForm param) {
        QueryWrapper<TBModuler> queryWrapper = new QueryWrapper();
        IPage<TBModuler> page = new Page<>(param.getCurrent(), param.getSize());
        /**
         * 订单基础表信息查询
         */
        IPage<TBModuler> list = tBModulerService.page(page, queryWrapper);
        /**
         * 将结果数据转换为-》MiniUITreeUtil类型，调用工具类行成树结构
         */
        List<MiniUITreeUtil> toTreeList = this.ListToTreeData(list);
        List<MiniUITreeUtil> treeResult = MiniUITreeUtil.getTreeResult(toTreeList);
        return Result.success(treeResult);


    }

    /**
     * 将角色数据转换为树结构
     *
     * @param tbModulers
     * @return
     */
    public List<MiniUITreeUtil> ListToTreeData(IPage<TBModuler> tbModulers) {
        List<MiniUITreeUtil> list = new ArrayList<>();
        for (TBModuler tbModuler : tbModulers.getRecords()) {
            MiniUITreeUtil miniUITreeUtil = new MiniUITreeUtil();
            miniUITreeUtil.setId(tbModuler.getId());
            miniUITreeUtil.setParenIid("-1");
            miniUITreeUtil.setText(tbModuler.getVModulerName());
            list.add(miniUITreeUtil);
        }
        return list;
    }
}

